/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cxhd.lis.ctrl;

import akka.actor.ActorRef;
import com.cxhd.lis.DataCache;
import com.cxhd.lis.ctrl.actor.ActorManager;
import com.cxhd.lis.model.LisSystemLog;
import com.cxhd.lis.model.newmodel.LisUser;
import com.cxhd.lis.util.CacheTool;
import com.cxhd.lis.util.QrCodeRender;
import com.jfinal.core.Controller;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.kit.HashKit;
import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import java.util.List;

/**
 *
 * @author 52461
 */
@ControllerBind(controllerKey = "/")
public class IndexCtrl extends Controller {

    private LisUser lisUser = new LisUser();
//    private LogService log = new LogService();

    public void index() {

        render("/login.jsp");
    }

    public void main() {
        render("/main.jsp");
    }

    public void error() {
        render("/authError.jsp");
    }

    public void login() {
        LisUser user = lisUser.checkLogin(getPara("userId"), getPara("password"));

        if (user != null) {

            List<Record> listAuth = Db.find("select b.url,b.icon_class from lis_system_role_res a left join lis_system_res b on a.res_id=b.pk where role_id=" + user.get("role_id"));

            setSessionAttr("user_pk", user.get("pk") + "");
            setSessionAttr("lisUser", user);
            setSessionAttr("role_id", user.get("role_id") + "");
            setSessionAttr("name", user.getStr("name"));
            setSessionAttr("userId", user.getStr("user_id"));
            setSessionAttr("auth", JsonKit.toJson(listAuth));
            CacheTool.saveAuthToCache(user.get("pk") + "", listAuth);

           // System.out.println(DataCache.getIntance().getUserAuth(user.get("pk") + "") + "------------");
//            log.insert(this, LisSystemLog.EVENT_LOGIN);
            renderText("true");
        } else {
            renderText("false");
        }
    }

    public void testqr() {

        render(new QrCodeRender(getPara("data")));
    }

    public void testSend() {
        List<Record> list = Db.find("select * from lis_samplecollection where UPDATE_STATUS=0 ");
        if (list != null && !list.isEmpty()) {
            for (Record re : list) {
                ActorManager.getIntance().getActor("SendDataActor").tell(re.get("PK") + "", ActorRef.noSender());
            }
        }

        renderText("true");
    }

}
